<template>
  <div class="wrapper" ref="scroll">
    <div class="content">
      <div class="city-hot">
        <div class="hot-top">
          热门城市
        </div>
        <div class="hot-list">
          <div class="hot-item" v-for="item in hotCity" @click="hotClick(item.hotCity)">{{ item.hotCity }}</div>
        </div>
      </div>
      <div class="city-sort">
        <div class="sort-top">
          字母排序
        </div>
        <div class="sort-list">
          <div class="sort-item" v-for="item in sortList" @click="sortClick(item.belong)">{{ item.belong }}</div>
        </div>
      </div>
      <div class="city-letters" v-for="(item,index) in cityLetters" :key="index" :ref="item.code">
        <div class="letters-top">
          {{ item.code }}
        </div>
        <div class="letters-list">
          <div class="letters-item" ref="sort" v-for="(val,i) in item.arrayList" :key="i" @click="hotClick(val.name)">{{ val.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import {mapState,mapMutations} from 'vuex'
export default {
  name: "CitySort",
  props: {
    hotCity: {
      type: Array,
      default() {
        return []
      }
    },
    sortList: {
      type: Array,
      default() {
        return []
      }
    },
    cityLetters: {
      type: Array,
      default() {
        return []
      }
    },
  },
  methods: {
    ...mapMutations(['changeCity']),
    sortClick(key) {
      this.$emit('sortClick',key)
      this.scroll.scrollToElement(this.$refs[key][0],500)
    },
    hotClick(cityName) {
      this.changeCity(cityName)
      this.$router.push('/home')
    },
    hotClick(cityName) {
      this.changeCity(cityName)
      this.$router.push('/home')
    }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.scroll,{
      click:true,
      pullUpLoad:true
    })
  },
  updated () {
    //重新计算高度
    this.scroll.refresh();
    //当数据加载完毕以后通知better-scroll
    this.scroll.finishPullUp();
  }
}
</script>

<style scoped>
  .hot-top {
    font-size: .28rem;
    line-height: .6rem;
    padding-left: .2rem;
    color: #212121;
  }
  .hot-list {
    width: 100%;
    background: #fff;
    overflow: hidden;
  }
  .hot-item {
    position: relative;
    width: 33.333%;
    float: left;
    font-size: .3rem;
    text-align: center;
    line-height: .88rem;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .hot-item::before {
    position: absolute;
    content: '';
    width: .02rem;
    height: 100%;
    right: 0;
    background: #f1f1f1;
  }
  .hot-item::after {
    position: absolute;
    content: '';
    width: 100%;
    height: 1px;
    left: 0;
    bottom: 0;
    background: #f1f1f1;
  }
  .sort-top {
    font-size: .28rem;
    line-height: .6rem;
    padding-left: .2rem;
    color: #212121;
  }
  .sort-list {
    width: 100%;
    background: #fff;
    overflow: hidden;
  }
  .sort-item {
    position: relative;
    width: 16.666%;
    float: left;
    font-size: .3rem;
    text-align: center;
    line-height: .88rem;
  }
  .letters-top {
    font-size: .28rem;
    line-height: .6rem;
    padding-left: .2rem;
    color: #212121;
  }
  .letters-list {
    width: 100%;
    background: #fff;
    overflow: hidden;
  }
  .letters-item {
    position: relative;
    width: 25%;
    float: left;
    font-size: .3rem;
    text-align: center;
    line-height: .88rem;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .letters-item::before {
    position: absolute;
    content: '';
    width: .02rem;
    height: 100%;
    right: 0;
    background: #f1f1f1;
  }
  .letters-item::after {
    position: absolute;
    content: '';
    width: 100%;
    height: 1px;
    left: 0;
    bottom: 0;
    background: #f1f1f1;
  }
</style>
